package net.posick.ws.soap;

import java.io.Serializable;
import java.util.List;

import net.posick.ws.xml.Name;
import net.posick.ws.xml.Namespace;
import net.posick.ws.xml.XmlElement;

public class SOAPHeader extends SOAPBase implements Serializable
{
    private static final long serialVersionUID = 201403271221L;
    
    
    public static final String ELEMENT_NAME = "Header";
    
    public static final Name FQN = new Name(new Namespace(PREFIX_SOAP_1_1, NAMESPACE_SOAP_1_1), ELEMENT_NAME);
    
    
    public SOAPHeader()
    {
        super(FQN);
    }
    
    
    public SOAPHeader(XmlElement... xmls)
    {
        super(FQN);
        for (XmlElement xml : xmls)
        {
            if (xml != null)
            {
                addChild(xml);
            }
        }
    }
    
    
    public static SOAPHeader toSOAPHeader(XmlElement xml)
    throws SOAPException
    {
        SOAPHeader header;
        
        Name name = xml.getName();
        
        if (FQN.equals(name))
        {
            header = new SOAPHeader();
            List<XmlElement> elements = xml.getChildren();
            if (elements != null && elements.size() > 0)
            {
                for (XmlElement element : elements)
                {
                    header.addChild(element);
                }
            } else
            {
                throw new SOAPException("Invalid WS-I Basic Profice 1.2 SOAP Header \"" + xml.getName() + "\". " + ERROR_MESSAGE_R9981);
            }
        } else
        {
            throw new SOAPException("Invalid WS-I Basic Profice 1.2 SOAP Header \"" + xml.getName() + "\".  Must be of type \"" + FQN + "\"");
        }
        
        return header;
    }
}
